package com.accesshollywood.grfxOrdering;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import com.accesshollywood.SQL.GFXSQL;
import com.accesshollywood.components.FoldablePanel;
import com.accesshollywood.grfxOrdering.Dialogs.CommDialog;
import com.accesshollywood.grfxOrdering.Dialogs.EditOrderDialog;
import com.accesshollywood.grfxOrdering.Dialogs.PreApprovedDialog;
import com.accesshollywood.grfxOrdering.Dialogs.PrefsDialog;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.grfxOrdering.panels.OrderListPanel;
import com.accesshollywood.indexables.GrfxOrder;
import com.accesshollywood.trace.Trace;

public class OrderMainGUI extends JFrame {
	/**
	 * 
	 */
	private static final long				serialVersionUID	= -1516727686100471575L;
	public static ArrayList<FoldablePanel>	allFoldables		= new ArrayList<FoldablePanel>();
	public static JPanel					contentPane;
	public JButton							btnEditOrder;
	public static OrderListPanel			orderListPanelTop;
	// public static OrderListPanel orderListPanelBottom;
	private JSplitPane						splitOrderList;
	EditOrderDialog							newOrderFrame;
	public static JFrame					thisFrame;
	public static JTextArea					txt_Messages;


	/**
	 * Create the frame.
	 */
	public OrderMainGUI() {
		thisFrame = this;
		if (Users.currentUser != null) {
			setTitle("ACCESS HOLLYWOOD GRAPHICS ORDER SYSTEM --- " + Users.currentUser.getFullName());
		}
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		resetFrame();

		contentPane = createInnerGUI();
	}
	
	public void resetFrame(){
		setBounds(100, 100, 1120, 780);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	public JPanel createInnerGUI() {
		JPanel innerPanel = new JPanel();
		innerPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(innerPanel);
		innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));

		JPanel searchPanel = new JPanel();
		innerPanel.add(searchPanel);
		searchPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.X_AXIS));

		JButton btnNewOrder = new JButton("NEW ORDER");
		btnNewOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				newOrderPanel();
			}
		});
		btnNewOrder.setEnabled(Users.currentUser.isAdmin() || Users.currentUser.isOrderer() || Users.currentUser.isPhotoEdit());

		btnEditOrder = new JButton("EDIT ORDER");
		btnEditOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				editOrderPanel(orderListPanelTop.getSelectedOrder());
			}
		});
		btnEditOrder.setEnabled(false);

		Component horizontalGlue = Box.createHorizontalGlue();
		horizontalGlue.setPreferredSize(new Dimension(155, 0));
		horizontalGlue.setMinimumSize(new Dimension(155, 0));
		horizontalGlue.setMaximumSize(new Dimension(155, 0));
		searchPanel.add(horizontalGlue);
		searchPanel.add(btnNewOrder);
		searchPanel.add(btnEditOrder);

		searchPanel.add(Box.createHorizontalGlue());

		JButton btnNameDB = new JButton("Pre-Approved Spellings");
		btnNameDB.setAlignmentX(Component.RIGHT_ALIGNMENT);
		btnNameDB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				PreApprovedDialog preApprovedNameDialog = new PreApprovedDialog(GRFXOrdersMain.gui, Dialog.ModalityType.MODELESS);
				// test();
			}
		});
		searchPanel.add(btnNameDB);

		JButton btnPrefs = new JButton("Prefs");
		btnPrefs.setAlignmentX(Component.RIGHT_ALIGNMENT);
		btnPrefs.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				PrefsDialog prefsDialog = new PrefsDialog(GRFXOrdersMain.gui, Dialog.ModalityType.MODELESS);
				// test();
			}
		});
		searchPanel.add(btnPrefs);

		if (Users.currentUser.isAdmin()) {
			JButton btnComms = new JButton("Comms");
			btnComms.setAlignmentX(Component.RIGHT_ALIGNMENT);
			btnComms.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					CommDialog commDialog = new CommDialog(GRFXOrdersMain.gui, Dialog.ModalityType.MODELESS);
					// test();
				}
			});
			searchPanel.add(btnComms);
		}
/*
		splitOrderList = new JSplitPane();
		splitOrderList.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitOrderList.setAlignmentX(Component.LEFT_ALIGNMENT);
		splitOrderList.setOneTouchExpandable(true);
		splitOrderList.setResizeWeight(0.75);
		splitOrderList.setDividerLocation(1000);
		splitOrderList.setDividerSize(10);
		innerPanel.add(splitOrderList);
*/
		orderListPanelTop = new OrderListPanel(this);
		// orderListPanelBottom = new OrderListPanel(this);
//		splitOrderList.setTopComponent(orderListPanelTop);
		// splitOrderList.setBottomComponent(orderListPanelBottom);

		innerPanel.add(orderListPanelTop);
		
		txt_Messages = new JTextArea();
		txt_Messages.setVisible(false);
		txt_Messages.setBorder(new TitledBorder(null, "Messages", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		txt_Messages.setPreferredSize(new Dimension(4, 100));
		txt_Messages.setMinimumSize(new Dimension(4, 300));
		txt_Messages.setMaximumSize(new Dimension(2147483647, 100));
		innerPanel.add(txt_Messages);

		return innerPanel;
	}

	public void showAddEdit(boolean editable) {
		// Trace.info("showing addEdit " + editable);
	}

	private void test() {
	}

	public void newOrderPanel() {
		newOrderFrame = new EditOrderDialog(this);
		newOrderFrame.setVisible(true);
	}

	public void editOrderPanel(GrfxOrder currOrder) {
		newOrderFrame = new EditOrderDialog(this, currOrder);
		newOrderFrame.setVisible(true);
	}

	public void addOrder(GrfxOrder currOrder) throws SQLException {
		// switch (mode) {
		// case NEW_ORDER:
		// GFXSQL.addOrder(currOrder);
		// break;
		// case EDIT_ORDER:
		// GFXSQL.removeOrder(currOrder.getIdOrder());
		// GFXSQL.addOrder(currOrder, currOrder.getIdOrder());
		// break;
		// case ORIGINAL_ORDER:
		// break;
		// }

		orderListPanelTop.refreshOrderModel();
		// orderListPanelBottom.refreshOrderModel();
	}

	public void editOrder(GrfxOrder currOrder) throws SQLException {
		GFXSQL.removeOrder(currOrder.getId());
		// GFXSQL.addOrder(currOrder, currOrder.getId());

		orderListPanelTop.refreshOrderModel();
		// orderListPanelBottom.refreshOrderModel();
	}

	public static void setFoldablesMaxHeight() {
		/*
		Trace.stack();
		thisFrame.repaint();
		// for (int i = 0; i < allFoldables.size(); i++) {
		for (FoldablePanel currFold : allFoldables) {
			currFold.revalidate();
			currFold.repaint();
			int a = currFold.getBounds().height;
			int b = currFold.foldMax;
			currFold.foldMax = Math.max(currFold.getBounds().height, currFold.foldMax);
			System.out.println(currFold.foldMax);
		}
		*/
	}

	public static void refresh(boolean fromSQL) {
		Trace.stack();
		thisFrame.repaint();
		if (orderListPanelTop != null) {
			orderListPanelTop.refreshOrderGUI(fromSQL);
		}
		// if (orderListPanelBottom != null) {
		// orderListPanelBottom.refreshOrderGUI(fromSQL);
		// }
		// thisFrame.setLocationRelativeTo(null);
		//setFoldablesMaxHeight();
	}

	public static void refresh(String fieldName, int[] orderNumbers, boolean fromSQL) {
		boolean doneTop = false;
		boolean doneBottom = false;
		if (orderNumbers != null) {
			for (int i = 0; i < orderNumbers.length; i++) {
				if (orderListPanelTop.orderNumbers.contains(orderNumbers[i])) {
					orderListPanelTop.refreshOrderGUI(fieldName, orderNumbers[i], fromSQL);
					doneTop = true;
				}
				// if (orderListPanelBottom.orderNumbers.contains(orderNumbers[i])) {
				// orderListPanelBottom.refreshOrderGUI(fieldName, orderNumbers[i], fromSQL);
				// doneBottom = true;
				// }
				if (doneTop) {// && doneBottom) {
					break;
				}
			}
		} else {
			orderListPanelTop.refreshOrderGUI(fromSQL);
			// orderListPanelBottom.refreshOrderGUI(fromSQL);
		}
		// setFoldablesMaxHeight(); // I think we should only do this if we change the ORDER FOLDABLE part
	}

	public static void refresh(int[] orderNumbers, boolean fromSQL) {
		boolean doneTop = false;
		boolean doneBottom = false;

		for (int i = 0; i < orderNumbers.length; i++) {
			if (orderListPanelTop.orderNumbers.contains(orderNumbers[i])) {
				orderListPanelTop.refreshOrderGUI(fromSQL);
				doneTop = true;
			}
			// if (orderListPanelBottom.orderNumbers.contains(orderNumbers[i])) {
			// orderListPanelBottom.refreshOrderGUI(fromSQL);
			// doneBottom = true;
			// }
			if (doneTop) {// && doneBottom) {
				break;
			}
		}
		setFoldablesMaxHeight();
	}
}
